package net.ermannofranco.genericdaojdbc.util;

public class Fn {

	private final String alias;
	private final String column;
	private final StatFunc func;

	private Fn(StatFunc func, String column, String alias) {
		this.func = func;
		this.column = column;
		this.alias = alias;
	}

	public static Fn AVG(String column, String alias) {
		return new Fn(StatFunc.AVG, column, alias);
	}

	public static Fn SUM(String column, String alias) {
		return new Fn(StatFunc.SUM, column, alias);
	}

	public static Fn MIN(String column, String alias) {
		return new Fn(StatFunc.MIN, column, alias);
	}

	public static Fn MAX(String column, String alias) {
		return new Fn(StatFunc.MAX, column, alias);
	}

	public static Fn COUNT(String column, String alias) {
		return new Fn(StatFunc.COUNT, column, alias);
	}

	public static Fn get(StatFunc f, String column, String alias) {
		switch (f) {
		case AVG:
			return AVG(column, alias);
		case MAX:
			return MAX(column, alias);
		case SUM:
			return SUM(column, alias);
		case MIN:
			return MIN(column, alias);
		case COUNT:
			return COUNT(column, alias);
		default:
			throw new IllegalArgumentException(f + " not supported.");
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(Util.SPACE);
		sb.append(func.name()).append(Util.BRACKET_OPEN).append(column).append(
				Util.BRACKET_CLOSE_W_SPACE).append(alias).append(Util.SPACE);
		return sb.toString();
	}

}
